<?php

namespace app\common\libs;

use yiqiniu\extend\facade\Extend;

/**
 * 导出
 */
class Excel
{

    private static $_instance = null;

    //文件名
    protected $filename = '';

    //导出数据
    protected $excel_data = null;

    //标题名称
    protected $excel_titles = null;

    //导出类型
    protected $send_type = 'excel';


    public static function getInstance()
    {

        if (self::$_instance === null) {
            self::$_instance = new static();
        }

        return self::$_instance;
    }

    //名称
    public function filename(string $filename)
    {
         $this->path_name = $filename;
        return $this;
    }


    //导出数据
    public function excelData($excel_data)
    {
        $this->excel_data = $excel_data;
        return $this;
    }

    //列表名称
    public function excelTitles($excel_titles)
    {
        $this->excel_titles = $excel_titles;
        return $this;
    }

    //导出类型
    public function sendType(string $type)
    {
        $this->send_type = $type;
        return $this;
    }

    //导出
    public function send(){
        switch ($this->send_type){
            case 'excel':
                $this->excelSend();
                break;
            case 'pdf':
                $this->PdfSend();
                break;
                default;
                    return false;
        }
        return  true;
    }


    /**
     * excelsend导出
     */
    private function excelSend(): void
    {
        Extend::ExportFile()->exportExcel($this->path_name, $this->excel_titles, $this->excel_data);
    }

    /**
     * 导出PDF
     */
    private function PdfSend(): void
    {
        Extend::ExportFile()->exportPdf($this->path_name, $this->excel_data ,$this->excel_titles);

    }

}
